package com.caocaog.market.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.caocaog.market.entity.dto.ShopTypeTree;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.apache.ibatis.type.JdbcType;
import org.jeecg.common.handler.IntegerToBooleanHandler;
import org.jeecg.common.handler.ListToStringHandler;
import org.jeecg.common.system.base.entity.AllEntity;
import org.jeecg.common.system.base.entity.groups.InsertGroup;
import org.jeecg.common.system.base.entity.groups.UpdateGroup;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * 店铺配置
 *
 * @author chechen
 * @date 2022-11-03
 */
@Data
@Accessors(chain = true)
@TableName("mk_shop_type")
@EqualsAndHashCode(callSuper = false)
public class ShopType extends AllEntity {


    @TableId(type = IdType.AUTO)
    @NotNull(message = "id不能为空", groups = UpdateGroup.class)
    private Long id;


    /**
     * 父级id 默认为0
     */
    private Long parentId;


    /**
     * 店铺分类名称
     */
    @NotBlank(message = "店铺分类名称不能为空", groups = InsertGroup.class)
    private String name;


    /**
     * 是否有子级 0没有 1有
     */
    @TableField(jdbcType = JdbcType.VARCHAR, insertStrategy = FieldStrategy.NOT_NULL, typeHandler = IntegerToBooleanHandler.class)
    private Boolean haveChildren;


    /**
     * 分类级别，1一级 2二级 3三级 最多三级，三级无法创建子集
     */
    private Integer level;


    /**
     * 排序
     */
    private Long sort;


    /**
     * 状态 0冻结 1激活
     */
    @TableField(jdbcType = JdbcType.VARCHAR, insertStrategy = FieldStrategy.NOT_NULL, typeHandler = IntegerToBooleanHandler.class)
    private Boolean status;


    /**
     * 分站黑名单（varchar400的长度可存储20个地区分站id）
     * 被加入黑名单的分站无法看到此节点以及旗下的所有分支
     */
    @Size(max = 20, message = "最多添加20个分站黑名单", groups = {InsertGroup.class, UpdateGroup.class})
    @TableField(jdbcType = JdbcType.VARCHAR, insertStrategy = FieldStrategy.NOT_NULL, typeHandler = ListToStringHandler.class)
    private List<String> blacklist;


    @Override
    public void clearFields() {
        super.clearFields();
        this.haveChildren = null;
        this.level = null;
        this.status = null;
    }


    public ShopTypeTree convertShopTypeTree() {
        return new ShopTypeTree()
                .setId(this.id)
                .setParentId(this.parentId)
                .setName(this.name)
                .setLevel(this.level)
                .setSort(this.sort)
                .setStatus(this.status)
                .setBlacklist(this.blacklist);
    }

}
